<?php

namespace app\api\controller;

use app\common\model\Attachment as AttachmentModel;
use app\model\Article as ArticleModel;
use think\Db;
use think\facade\Log;
use think\facade\Request;

/**
 * @title   文章接口
 * @desc    文章接口
 * Class Article
 * @package app\api\controller
 */

class Article extends Common
{
    /**
     * @title 文章列表
     * @desc  文章列表
     *
     * @param int $title
     * @param int $limit 10
     *
     * @return Array $list 文章列表
     */
    public function lists()
    {
        // try {
        //     $msg = $this->Auth();
        //     if (!$msg) {
        $title = $this->request->param('title');
        $cname = $this->request->param('cname');
        $limit = $this->request->param('limit');
        if ($title != '') {
            $map[] = ['title', 'like', '%' . $title . '%'];
        }
        if (!$limit) {
            $limit = 10;
        }
        // if ($cname != '') {
        //     $cnamearr[] = ['cname', $cname];
        // }
        $list = ArticleModel::where($map)
            ->where('cname', $cname)
            ->field(['id', 'cname', 'title', 'cover', 'create_time'])
            ->order('id desc')
            ->paginate($limit);

        foreach ($list as $gq) {
            $Attachment = AttachmentModel::get($gq['cover']);
            $gq['cover'] = $this->getPicPath($Attachment['path']);
        }
        return $this->jsonPage($list->total(), $list->toArray()['data']);

    }

    /**
     * 文章内容
     * @return string
     */
    public function info()
    {
        // if ($this->request->isPost()) {
        //     $msg = $this->Auth();
        //     if (!$msg) {
        $id = $this->request->param('id');
        $data = ArticleModel::get($id, 'articleData');
        //, 'codename', 'plannum', 'title', 'ename', 'classname', 'ics', 'ccs', 'oldcode', 'revision', 'property', 'flag', 'release', 'implement', 'department', 'technical', 'drafting', 'draftsman'
        // if (!empty($data->content->content)) {
        //     $data->content->content = html_entity_decode($data->content->content);
        // }

        $Attachment = AttachmentModel::get($data['cover']);
        $data['cover'] = $this->getPicPath($Attachment['path']);

        return $this->jsonSuccess('成功', $data);
        //     } else {
        //         return $msg;
        //     }
        // } else {
        //     return $this->jsonStop();
        // }
    }

}
